<?php


namespace app\job;

use app\admin\model\Equity;
use app\admin\model\Order;
use app\admin\model\UserRelation;
use app\admin\model\User;
use app\admin\model\UserDayReport;
use think\facade\Log;
use think\queue\Job;

class DayReportJob
{
    /**
     *分润
     */
    public function fire(Job $job, $data)
    {
        $isJobDone = $this->doTask($data);
        if ($isJobDone) {
            $job->delete();
        } else {
            $job->release(2);
        }
    }

    /**
     *
     * task具体执行逻辑
     */
    public function doTask($data)
    {
        // $params=FXApiService::regist($data['mobile'],$data['name'],$data['password']);
        //$res = Db::table('chd_auth_role')->insert($params);
        $order = new Order();
        $order_count = $order->where('user_id', $data['user_id'])->where('createtime', '>=', strtotime($data['ts'] . '00:00:00'))->where('createtime', '<=', strtotime($data['ts'] . '23:59:59'))->count();
        $equityModel = new Equity();
        $equityList = $equityModel->where('createtime', '>=', strtotime($data['ts'] . '00:00:00'))->where('createtime', '<=', strtotime($data['ts'] . '23:59:59'))->where('user_id', $data['user_id'])->select();
        $money = $forzen_money = $my_money = $rec_money = $retreat_count = 0;
        if ($equityList) {
            foreach ($equityList as $l => $m) {

                if ($m['status'] == 0 || $m['status'] == 1) {
                    $forzen_money += $m['fee'];
                }
                if ($m['status'] == 2) {
                    $money += $m['fee'];
                    if ($m['unit_percentage'] == 100) {
                        $my_money += $m['fee'];
                    } else {
                        $rec_money += $m['fee'];
                    }
                }
                if ($m['status'] == 4) {
                    $retreat_count++;
                }
            }
        }
        Log::info("==任务执行ok==" . json_encode($data));
        $user_count = User::where('rec_user_id', $data['user_id'])->where('createtime', '>=', strtotime($data['ts'] . '00:00:00'))->where('createtime', '<=', strtotime($data['ts'] . '23:59:59'))->count();
  
        $userModel = new UserDayReport();
        $litime=date('Ymd', strtotime($data['ts'] . '00:00:00'));
        $userFind=$userModel->where('user_id',$data['user_id'])->where('date',(int)$litime )->find();
        if($userFind){
            $userFind->money=$money;
            $userFind->order_count=$order_count;
            $userFind->my_money=$my_money;
            $userFind->rec_money=$rec_money;
            $userFind->forzen_money=$forzen_money;
            $userFind->retreat_count=$retreat_count;
            $userFind->rec_count=$user_count;
            $userFind->updatetime=time();
            $li=$userFind->save();
        }else{

            $arr = [
                'user_id' => $data['user_id'],
                'rec_count' => $user_count,
                'date' => (int)$litime,
                'money' => $money,
                'order_count' => $order_count,
                'my_money' => $my_money,
                'rec_money' => $rec_money,
                'forzen_money' => $forzen_money,
                'retreat_count' => $retreat_count,
                'createtime' => time()
            ];
            $li = $userModel->create($arr);
        }
      
        if ($li) {

            return true;
        } else {
            return false;
        }
    }
}
